$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/commodity/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'commodityId', index: 'commodity_id', width: 50, key: true},
            {label: '商品名称', name: 'commodityName', index: 'commodity_name', width: 80},
            {label: '商品描述', name: 'commodityDescribe', index: 'commodity_describe', width: 120},
            {
                label: '商品价格', name: 'price', index: 'price', width: 40, formatter: function (value, options, row) {
                    return value;
                }
            },
            {
                label: '类型一级分类', name: 'typeOneCode', index: 'type_one_code', width: 40,
                formatter: function (value, options, row) {
                    if (value === 1) {
                        return '<span class="label label-success">美发</span>';
                    }
                    if (value === 2) {
                        return '<span class="label label-primary">美容</span>';
                    }
                    if (value === 3) {
                        return '<span class="label label-success">美甲</span>';
                    }
                    if (value === 4) {
                        return '<span class="label label-success">纹身</span>';
                    }else {
                        return '<span class="label label-success">未知</span>';
                    }
                }
            },
            // {label: '类型二级分类', name: 'typeTwoCode', index: 'type_two_code', width: 80},
            {label: '创建时间', name: 'createTime', index: 'create_time', width: 80},
            // {label: '修改时间', name: 'updateTime', index: 'update_time', width: 80},
            // {label: '商品图片url多张用逗号隔开', name: 'imgurl', index: 'imgUrl', width: 80},
            // {label: '所属设计师编码 可以没有可以属于店铺', name: 'stylistCode', index: 'stylist_code', width: 80},
            // {label: '店铺id', name: 'storeId', index: 'store_id', width: 80},
            {
                label: '服务群体', name: 'serveType', index: 'serve_type', width: 40,
                formatter: function (value, options, row) {
                    if (value === 1) {
                        return '<span class="label label-success">女士</span>';
                    }
                    if (value === 2) {
                        return '<span class="label label-primary">男生</span>';
                    }
                    if (value === 3) {
                        return '<span class="label label-success">儿童</span>';
                    }else {
                        return '<span class="label label-success">未知</span>';
                    }
                }
            },
            {
                label: '删除状态', name: 'deleteType', index: 'delete_type', width: 40,
                formatter: function (value, options, row) {
                    if (value === 0) {
                        return '<span class="label label-success">未删除</span>';
                    }
                    if (value === 1) {
                        return '<span class="label label-primary">已删除</span>';
                    }
                }
            },
            {label: '已卖出数量', name: 'sellNumber', index: 'sell_number', width: 50},
            {
                label: '技师等级', name: 'technicianLevel', index: 'technician_level', width: 30,
                formatter: function (value, options, row) {
                    if (value === 1) {
                        return '<span class="label label-success">一星</span>';
                    }
                    if (value === 2) {
                        return '<span class="label label-primary">二星</span>';
                    }
                    if (value === 3) {
                        return '<span class="label label-success">三星</span>';
                    }
                    if (value === 4) {
                        return '<span class="label label-success">普通技师</span>';
                    }else {
                        return '<span class="label label-success">未知</span>';
                    }
                }
            },
            {
                label: '优惠价格',
                name: 'discounts',
                index: 'discounts',
                width: 40,
                formatter: function (value, options, row) {
                    return value;
                }
            }
            // {
            //     label: '上架状态', name: 'putAwayStatus', index: 'put_away_status', width: 80,
            //     formatter: function (value, options, row) {
            //         if (value === 1) {
            //             return '<span class="label label-success">已上架</span>';
            //         }
            //         if (value === 2) {
            //             return '<span class="label label-primary">未上架</span>';
            //         }
            //     }
            // }
            // {label: '作品类型 1 商品 2 帖子', name: 'productionType', index: 'production_type', width: 80}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        q:{
            commodityName : null
        },
        commodity: {}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.commodity = {};
        },
        update: function (event) {
            var commodityId = getSelectedRow();
            if (commodityId == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(commodityId)
        },
        saveOrUpdate: function (event) {
            var url = vm.commodity.commodityId == null ? "sys/commodity/save" : "sys/commodity/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.commodity),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var commodityIds = getSelectedRows();
            if (commodityIds == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/commodity/delete",
                    contentType: "application/json",
                    data: JSON.stringify(commodityIds),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (commodityId) {
            $.get(baseURL + "sys/commodity/info/" + commodityId, function (r) {
                vm.commodity = r.commodity;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData:{
                    'commodityName': vm.q.commodityName
                },
                page: page
            }).trigger("reloadGrid");
        }
    }
});